package com.ruoyi.law.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.law.domain.dto.LawServiceDTO;
import com.ruoyi.law.domain.entity.LawService;
import com.ruoyi.law.domain.entity.LawServicePrice;
import com.ruoyi.law.enums.ServiceType;
import com.ruoyi.law.service.ILawServicePriceService;
import com.ruoyi.law.service.ILawServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 咨询和服务 前端控制器
 * </p>
 *
 * @author code
 * @since 2022-11-25
 */
@Api(tags = "咨询和服务")
@RestController
@RequestMapping("/lawService")
public class LawServiceController extends BaseController {

    @Autowired
    private ILawServiceService lawServiceService;
    @Autowired
    private ILawServicePriceService lawServicePriceService;

    @Anonymous
    @ApiOperation("列表(不分页)")
    @ApiImplicitParam(name = "type", value = "服务类型（1.咨询 2.服务 不传查所有）")
    @GetMapping("/list")
    public R<List<LawService>> consultingList(String type) {
        LambdaQueryWrapper<LawService> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(type), LawService::getType, type);
        wrapper.orderByAsc(LawService::getSort);
        wrapper.orderByDesc(LawService::getCreateTime);
        List<LawService> list = lawServiceService.list(wrapper);
        return R.ok(list);
    }

    @Anonymous
    @ApiOperation("详情")
    @GetMapping("/info")
    public R<LawServiceDTO> info(Long id) {
        LawService byId = lawServiceService.getById(id);
        LawServiceDTO lawServiceDTO = BeanUtil.toBean(byId, LawServiceDTO.class);
        // 如果是咨询,查询价格列表
        if (ServiceType.CONSULTING.getType().equals(byId.getType())) {
            List<LawServicePrice> list = lawServicePriceService.lambdaQuery()
                    .eq(LawServicePrice::getAdviceId, id).list();
            lawServiceDTO.setLawServicePrice(list);
        }
        return R.ok(lawServiceDTO);
    }

    @ApiOperation("新增咨询")
    @PostMapping("/saveConsulting")
    public R<Object> saveConsulting(@RequestBody LawService lawService) {
        lawService.setType(ServiceType.CONSULTING.getType());
        lawServiceService.save(lawService);
        return R.ok();
    }

    @ApiOperation("新增服务")
    @PostMapping("/saveService")
    public R<Object> saveService(@RequestBody LawService lawService) {
        lawService.setType(ServiceType.SERVICE.getType());
        lawServiceService.save(lawService);
        return R.ok();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody LawService lawService) {
        lawServiceService.updateById(lawService);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long id) {
        lawServiceService.removeById(id);
        return R.ok();
    }

}
